# Maintainer: Markus Mützel <markus.muetzel@gmx.de>

_realname=octave
pkgbase=mingw-w64-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-${_realname}
pkgver=10.1.0
pkgrel=2
pkgdesc="GNU Octave: Interactive programming environment for numerical computations (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.octave.org"
msys2_repository_url="https://github.com/gnu-octave/octave"
msys2_references=(
  'archlinux: octave'
)
license=('spdx:GPL-3.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran")
         "${MINGW_PACKAGE_PREFIX}-arpack"
         "${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-fftw"
         "${MINGW_PACKAGE_PREFIX}-fontconfig"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-ghostscript"
         "${MINGW_PACKAGE_PREFIX}-gl2ps"
         "${MINGW_PACKAGE_PREFIX}-glpk"
         "${MINGW_PACKAGE_PREFIX}-graphicsmagick"
         "${MINGW_PACKAGE_PREFIX}-hdf5"
         "${MINGW_PACKAGE_PREFIX}-libiconv"
         "${MINGW_PACKAGE_PREFIX}-libsndfile"
         "${MINGW_PACKAGE_PREFIX}-omp"
         "${MINGW_PACKAGE_PREFIX}-openblas"
         "${MINGW_PACKAGE_PREFIX}-pcre2"
         "${MINGW_PACKAGE_PREFIX}-qhull"
         "${MINGW_PACKAGE_PREFIX}-qrupdate"
         $([[ ${CARCH} == i686 ]] && echo \
           "${MINGW_PACKAGE_PREFIX}-qscintilla-qt5" \
           "${MINGW_PACKAGE_PREFIX}-qt5-base" \
           "${MINGW_PACKAGE_PREFIX}-qt5-tools" || echo \
           "${MINGW_PACKAGE_PREFIX}-qscintilla-qt6" \
           "${MINGW_PACKAGE_PREFIX}-qt6-base" \
           "${MINGW_PACKAGE_PREFIX}-qt6-tools")
         "${MINGW_PACKAGE_PREFIX}-readline"
         "${MINGW_PACKAGE_PREFIX}-suitesparse"
         "${MINGW_PACKAGE_PREFIX}-sundials"
         "texinfo"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-fc"
             "${MINGW_PACKAGE_PREFIX}-gnuplot"
             "${MINGW_PACKAGE_PREFIX}-gperf"
             "${MINGW_PACKAGE_PREFIX}-fltk"
             "${MINGW_PACKAGE_PREFIX}-portaudio"
             "${MINGW_PACKAGE_PREFIX}-rapidjson")
checkdepends=("unzip"
              "zip")
optdepends=("unzip: for decompressing .zip archives"
            "zip: for compressing .zip archives"
            "${MINGW_PACKAGE_PREFIX}-epstool: eps output with tight bounding box"
            "${MINGW_PACKAGE_PREFIX}-fltk: alternative plotting and dialogs"
            "${MINGW_PACKAGE_PREFIX}-gnuplot: alternative plotting"
            "${MINGW_PACKAGE_PREFIX}-portaudio: audio support")
source=(https://ftp.gnu.org/gnu/octave/octave-$pkgver.tar.xz{,.sig}
        "0002-mk-doc-cache-path.patch"
        "0003-no-community-support.patch"
        "0005-makeinfo-perl.patch")
validpgpkeys=('DBD9C84E39FE1AAE99F04446B05F05B75D36644B')  # John W. Eaton
sha256sums=('051d092fe7abbed4ed9b74c8c40f3dfb1b9aa9eede770ac6c7e9c8c9e895e0c9'
            'SKIP'
            'aa5bd559d9774abc0f0c930a606762d1e452cc90278d16d8ae83561c0cae3bf8'
            'e53af21ad087e10a2906506589aab89ab8b43f4e3e4994b4c28e4d8ae83639ad'
            '2df9666d75bb8dd4f549ec857c75aa3ec64b21fd3c9aa25a0d4127b284ec6822')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Np1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  cd "${_realname}-${pkgver}"

  apply_patch_with_msg \
    0002-mk-doc-cache-path.patch \
    0005-makeinfo-perl.patch

  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    apply_patch_with_msg \
      0003-no-community-support.patch
  fi

  autoreconf -fiv
}

build() {
  mkdir -p build-${MSYSTEM} && cd build-${MSYSTEM}

  declare -a _extra_config
  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    # Clang seems to require a different set of dllexport attributes than GCC.
    _extra_config+=("F77=${MINGW_PREFIX}/bin/flang"
                    "--disable-lib-visibility-flags")
    # The headers from some dependent libraries (graphicsmagick++?) cause a
    # torrent of warnings about deprecated declarations.  Sometimes to the
    # point that the build crashes with garbled characters in the terminal
    # output for some reason.  Same issue with dllimport attributes at
    # inlined functions in Qt6 headers. Just calling `make all -j2` again in
    # the build tree lets the compilation finish successfully.
    # Disable those warnings as a work-around.
    CXXFLAGS+=" -Wno-deprecated-declarations -Wno-ignored-attributes"
  fi

  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --enable-shared \
    --disable-static \
    --enable-relocate-all \
    --disable-docs \
    "${_extra_config[@]}" \
    octave_cv_fftw3_threads_lib="-lfftw3_omp" \
    octave_cv_fftw3f_threads_lib="-lfftw3f_omp" \
    JAVA_HOME=""

  PERL5SHELL="bash -l -c" make all
}

check() {
  cd "${srcdir}"/build-${MSYSTEM}
  make check || true
}

package() {
  make -C "build-${MSYSTEM}" DESTDIR="${pkgdir}" install

  install -D -m644 "${srcdir}"/${_realname}-${pkgver}/COPYING "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/COPYING

  # Remove full path reference
  local _PREFIX_WIN="$(cygpath -wm ${MINGW_PREFIX})"
  for pcfile in "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/*.pc; do
    sed -s "s|${_PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${pcfile}"
  done
}
